package com.lyshop.jinding.view;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lyshop.jinding.R;
import com.lyshop.jinding.bean.FreshListBean;
import com.lyshop.jinding.utils.TimeCount;
import com.zc.mychart.utils.FormatUtil;

public class FirstPayDialog extends MyDialog implements View.OnClickListener {

    private View rootView;
    private View view_top;
    private TextView tv_describe;
    private TextView tv_profit;
    private ImageView iv_no;
    private LinearLayout ll_yes;
    private TextView tv_submit;
    private TextView tv_min;
    private TextView tv_second;

    private FreshListBean mBean;

    private DialogListener mListener;
    private TimeCount.TickListener mTicketListener;

    public FirstPayDialog(Context context, DialogListener listener) {
        this(context, false, Gravity.BOTTOM);
        this.mListener = listener;
    }

    public void setFreshListBean(FreshListBean bean) {
        this.mBean = bean;
        init();
    }

    public FirstPayDialog(Context context, boolean isCancelAble, int gravity) {
        super(context, isCancelAble, gravity);
        Window w = this.getWindow();
        if (w != null) {
            w.setWindowAnimations(R.style.dialogTranslateAnim);
        }
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_first_pay, null);
        setContentView(rootView);

        view_top = rootView.findViewById(R.id.view_top);
        tv_describe = rootView.findViewById(R.id.tv_describe);
        tv_profit = rootView.findViewById(R.id.tv_profit);
        iv_no = rootView.findViewById(R.id.iv_no);
        ll_yes = rootView.findViewById(R.id.ll_yes);
        tv_min = rootView.findViewById(R.id.tv_min);
        tv_second = rootView.findViewById(R.id.tv_second);
        tv_submit = rootView.findViewById(R.id.tv_submit);

        view_top.setOnClickListener(this);
        tv_submit.setOnClickListener(this);

        mTicketListener = new TimeCount.TickListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                String[] timeArray = FormatUtil.secondToTimeByHHS(millisUntilFinished);
                tv_min.setText(timeArray[1]);
                tv_second.setText(timeArray[2]);
            }

            @Override
            public void onFinish() {
                NewUserModule.getInstance().stopCountDown();
            }
        };
    }

    private void init() {
        if (null != mBean) {
            //1首单成功，2首单失败
            if (mBean.send_type == 1) {
                tv_profit.setVisibility(View.VISIBLE);
                ll_yes.setVisibility(View.VISIBLE);
                iv_no.setVisibility(View.GONE);

                tv_describe.setText("赚钱啦！");
                tv_profit.setText("+" + mBean.amount + "元");
                tv_submit.setText("充值继续赚钱");
            } else {
                tv_profit.setVisibility(View.GONE);
                ll_yes.setVisibility(View.GONE);
                iv_no.setVisibility(View.VISIBLE);

                tv_describe.setText("您的[新手券]订单未增益");
                tv_submit.setText("再来一单");
            }
        }

        NewUserModule.getInstance().startCountDown(mTicketListener);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_top:
                if (isCancelAble) {
                    dismiss();
                }
                break;
            case R.id.tv_submit:
                if (null != mListener) {
                    mListener.onConfirm(mBean);
                }
                dismiss();
                break;
        }
    }


    public interface DialogListener {
        void onConfirm(FreshListBean bean);
    }


    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        NewUserModule.getInstance().stopCountDown();
    }
}

